package com.haipumi.servlets.backadmin;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.haipumi.beans.DataGrid;
import com.haipumi.beans.haipumi_user;
import com.haipumi.biz.IBackCustBiz;
import com.haipumi.biz.impl.BackCustBizImpl;
import com.haipumi.servlets.CommonServlet;
import com.haipumi.utils.RequestUtil;

public class BackCustServlet extends CommonServlet {

	IBackCustBiz ibcb=new BackCustBizImpl();
	/**
	 * 
	 */
	private static final long serialVersionUID = 1048978724338440500L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String op=null;
		op=request.getParameter("op");
		if(op!=null){
			if("findAllCust".equals(op)){
				findAllCust(request,response);
			}else if("saveCust".equals(op)){
				saveCust(request,response);
			}else if("editCust".equals(op)){
				editCust(request,response);
			}
		}
	}
	/**
	 * 编辑用户信息，包括密码，但以密文显示
	 * @param request
	 * @param response
	 */
	private void editCust(HttpServletRequest request,
			HttpServletResponse response) {
		haipumi_user backuser=null;
		try {
			haipumi_user user=RequestUtil.parseRequest(request, haipumi_user.class);
			backuser=ibcb.updateUser(user);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.outData(response, backuser);
	}
	/**
	 * 添加用户的操作
	 * @param request
	 * @param response
	 */
	private void saveCust(HttpServletRequest request,
			HttpServletResponse response) {
		boolean isNewRecord=Boolean.parseBoolean(request.getParameter("isNewRecord"));
		haipumi_user user=null;
		haipumi_user backuser=null;
		if(isNewRecord){
			//是添加操作
			try {
				user=RequestUtil.parseRequest(request, haipumi_user.class);
				backuser=ibcb.insertCust(user);
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			//是编辑操作
		}
		super.outData(response, backuser);
	}
	/**
	 * findAllCust的方法，所有的Cust查询都是调用这个方法，在此方法中有判断的方法是初始化还是查询的方法，以及查询的类型，是按模糊还是精确查找
	 * @param request
	 * @param response
	 */
	private void findAllCust(HttpServletRequest request,
			HttpServletResponse response) {
		List<haipumi_user>userlist=(List<haipumi_user>) ibcb.findCust(request);
		DataGrid gd=new DataGrid();
		gd.setRows(userlist);
		int r=ibcb.findusercount(request);
		gd.setTotal(r);
		super.outData(response, gd);
	}

}
